package br.ufpe.cin.rgms.membro.apresentacao;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import br.ufpe.cin.rgms.Facade;
import br.ufpe.cin.rgms.membro.controle.ControleMembro;
import br.ufpe.cin.rgms.membro.modelo.Membro;

public class LoginServlet extends HttpServlet {

	public void doPost( HttpServletRequest request, 
			HttpServletResponse response) 
	throws IOException, ServletException {
		String email = request.getParameter("email");
		String senha = request.getParameter("senha");
		Membro membro = new Membro(email, senha);
		
		if(Facade.getInstance().existsMembro(membro)){
			membro = Facade.getInstance().getMembro(email);
			request.getSession().setAttribute("usuario", membro);
			
			request.setAttribute("loginsuccess", Boolean.TRUE);
			
			RequestDispatcher view = request.getRequestDispatcher("home.jsp");
			view.forward(request, response);
		}
		else{
			request.setAttribute("loginsuccess", Boolean.FALSE);
			
			RequestDispatcher view = request.getRequestDispatcher("index.jsp");
			view.forward(request, response);
		}
	}
}
